﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;
using Obsidian.Action;
using Obsidian.Web;
using Obsidian.Edm;
using Obsidian.Runtime;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Data;

namespace Oak.MagSys.Logic
{
    public class MenuItemLG : OakLG<ISession>
    {

        public MenuItemLG(ISession session) : base(session) { }

        public List<MsMenuItemInfo> GetMenuItemList(MsMenuItemQry qry, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            MenuItemDA menuItemDA = new MenuItemDA();
            List<MsMenuItemInfo> list = menuItemDA.SelectList(qry);
            msg = "";
            return list;
        }

        public MsMenuItemInfo GetMenuItemById(long itemId, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            MsMenuItemInfo mi = new MsMenuItemInfo();
            if (!mi.GetByPrimaryKey(itemId))
            {
                msg = "该菜单信息不存在";
                return null;
            }
            msg = "";
            return mi;
        }

        public MsMenuItemInfo AddMenuItem(MsMenuItemInfo menuItem, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            if (menuItem.ParentId.IsNull || menuItem.ParentId.Value < 0)
                menuItem.ParentId.Set(0);

            long parentId = menuItem.ParentId.Value;
            if (parentId > 0)
            {
                MsMenuItemInfo parent = this.GetMenuItemById(parentId, out msg);
                if (parent == null)
                {
                    msg = "该父级菜单不存在";
                    return null;
                }
                menuItem.Depth.Set(parent.Depth.Value + 1);
            }
            else
            {
                menuItem.Depth.Set(0);
            }

            if(menuItem.SortOrder.IsNull)
                menuItem.SortOrder.Set(0);

            if (menuItem.IsSystemMenuItem.IsNull)
                menuItem.IsSystemMenuItem.Set(false);

            if (!VerifyMenuItem(menuItem, Session, out msg))
                return null;

            try
            {
                menuItem.Save();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "保存信息错误:" + code;
                return null;
            }

            msg = "";
            return menuItem;
        }

        public MsMenuItemInfo UpdateMenuItem(MsMenuItemInfo menuItem, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            if (menuItem.ItemId.IsNull)
            {
                msg = "菜单ID不能为空";
                return null;
            }
            MsMenuItemInfo dbItem = this.GetMenuItemById(menuItem.ItemId.Value, out msg);
            if (dbItem == null) return null;

            dbItem.ResetAssigned();
            if (!menuItem.FrameId.IsNull)
                dbItem.FrameId.Set(menuItem.FrameId.Value);
            if (!menuItem.ItemText.IsNull)
                dbItem.ItemText.Set(menuItem.ItemText.Value);
            if (!menuItem.IsSystemMenuItem.IsNull)
                dbItem.IsSystemMenuItem.Set(menuItem.IsSystemMenuItem.Value);
            if (!menuItem.SortOrder.IsNull)
                dbItem.SortOrder.Set(menuItem.SortOrder.Value);

            if (!VerifyMenuItem(menuItem, Session, out msg))
                return null;

            try
            {
                dbItem.Update();
                msg = "";
                return dbItem;
            }
            catch (Exception ex)
            {
                msg = "保存信息错误";
                return null;
            }

        }

        public bool DeleteMenuItem(long menuItemId, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return false;
            }

            MsMenuItemInfo menuItem = this.GetMenuItemById(menuItemId, out msg);
            if (menuItem == null) return false;

            try
            {
                menuItem.Delete();
                msg = "";
                return true;
            }
            catch (Exception ex)
            {
                msg = "操作失败";
                return false;
            }
        }

        internal static bool VerifyMenuItem(MsMenuItemInfo menuItem, ISession session, out string msg)
        {

            if (!menuItem.ParentId.IsNull && menuItem.ParentId.Value > 0)
            {
                if (menuItem.FrameId.IsNull)
                {
                    msg = "菜单框架属性不能为空";
                    return false;
                }
                FrameLG frameLG = new FrameLG(session);
                if (frameLG.GetFrameById(menuItem.FrameId.Value, out msg) == null)
                    return false;
            }

            msg = "";
            return true;
        }
    }
}
